package com.hzqx.syndata.falsedemo.controller;


import com.hzqx.syndata.falsedemo.service.TestCacheService;
import com.hzqx.syndata.result1.ResultBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZY
 * @since 2020-11-05
 */
@Slf4j
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {

    final private TestCacheService testCacheService;

    public SysConfigController(TestCacheService testCacheService) {
        this.testCacheService = testCacheService;
    }

    @GetMapping("/var")
    @Cacheable(cacheNames = "num",key = "#root.methodName+#code",sync=true)
    public String getData(String code){
        System.out.println(code+"开始查询");
        RestTemplate restTemplate = new RestTemplate();
        String url ="http://172.18.110.83:9095/flm/system/object/findFlmCodeNum?areaCode="+code;
        String num = restTemplate.getForObject(url,String.class);
        log.info(num);
        return num;
    }
    @GetMapping("/det")
    @Cacheable(cacheNames = "num",key = "#root.methodName+#areaCode+#codeColor",sync=true)
    public String getDeatail(@RequestParam("areaCode")String areaCode, @RequestParam("codeColor")String codeColor){
        System.out.println(areaCode+"区域码"+"颜色"+codeColor);
        try {
            RestTemplate restTemplate = new RestTemplate();
            String url ="http://172.18.110.83:9095/flm/system/object/findCompanyDetail?areaCode="+areaCode+"&codeColor="+codeColor;
            String result = restTemplate.getForObject(url,String.class);
            return result;
        }catch (Exception e){
            return ResultBuilder.error("接口异常").toString();
        }
    }
    @GetMapping("/del")
    @CacheEvict(cacheNames = "num",allEntries = true)
    public void getDataCompany(){
        System.out.println("删除所有缓存");
    }

    /**
     * 在service中写的缓存
     * @param
     * @return
     */
    @GetMapping("/var2")
    public String getDataVar1(@RequestParam("areaCode")String areaCode, @RequestParam("codeColor")String codeColor){

        String num = testCacheService.getDeatail(areaCode,codeColor);
        return num;
    }

    /**
     * 在service中写的缓存
     * @param code
     * @return
     */
    @GetMapping("/var1")
    public String getDetail1(String code){
        //System.out.println("var1fangfa"+code);
        String num = testCacheService.getData(code);
        return num;
    }


    @GetMapping("/exc")
    public String testcep(String code){
        //System.out.println("var1fangfa"+code);
        String num = testCacheService.testExcep("12345");
        return num;
    }
}
